package com.example.opengldemo.simpleGame.object;


import android.opengl.GLES30;
import android.opengl.GLSurfaceView;

import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;


public class TexRect  
{	
	int mProgram;
    int muMVPMatrixHandle;
    int muMMatrixHandle;
    int maCameraHandle;
    int maPositionHandle;
    int maTexCoorHandle;
    
    int uTexHandle;
    
    String mVertexShader;
    String mFragmentShader;
	
	FloatBuffer mVertexBuffer;
	FloatBuffer   mTextureBuffer;
    int vCount=0;   
    
    public TexRect(GLSurfaceView mv, int mProgramId, float size, float width, float height)
    {    	

    	initVertexData(size,width,height);
    	this.mProgram=mProgramId;
     	//intShader(mv);
    } 
    

    public void initVertexData(float UNIT_SIZE,float width,float height)
    {

        vCount=6;
        float vertices[]=new float[]
        {
        	-width*UNIT_SIZE,height*UNIT_SIZE,0,
        	-width*UNIT_SIZE,-height*UNIT_SIZE,0,
        	width*UNIT_SIZE,height*UNIT_SIZE,0,
        	
        	-width*UNIT_SIZE,-height*UNIT_SIZE,0,
        	width*UNIT_SIZE,-height*UNIT_SIZE,0,
        	width*UNIT_SIZE,height*UNIT_SIZE,0
        };
		


        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        float textures[]=new float[]
        {
        	0,0,0,1,1,0,
        	0,1,1,1,1,0
        };

        

        ByteBuffer tbb = ByteBuffer.allocateDirect(textures.length*4);
        tbb.order(ByteOrder.nativeOrder());
        mTextureBuffer= tbb.asFloatBuffer();
        mTextureBuffer.put(textures);
        mTextureBuffer.position(0);


    }  
      

    public void initShader()
    {
    	

        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");

        maTexCoorHandle=GLES30.glGetAttribLocation(mProgram, "aTexCoor");  

        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");   

    }
    
    public void drawSelf(int texId) 
    {        
    	   initShader();

    	 GLES30.glUseProgram(mProgram);

         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState3D.getFinalMatrix(), 0);
         

         GLES30.glVertexAttribPointer        
         (
         		maPositionHandle,   
         		3, 
         		GLES30.GL_FLOAT, 
         		false,
                3*4, 
                mVertexBuffer   
         );       

         GLES30.glVertexAttribPointer  
         (  
        		maTexCoorHandle,  
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                mTextureBuffer
         );   

         GLES30.glEnableVertexAttribArray(maPositionHandle);  
         GLES30.glEnableVertexAttribArray(maTexCoorHandle);  

         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);    
         GLES30.glUniform1i(uTexHandle, 0);
           

         GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount); 
    }
}
